package com.soup.view;

import com.soup.dao.PersonDao;
import com.soup.domain.Person;
import com.soup.utils.StringUtils;
import com.soup.view.component.Context;
import com.soup.view.component.MassageTip;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.List;

public class UpdateFrame extends Context implements KeyListener, ActionListener {

    private JTextField search = new JTextField(10);

    private JTextField id = new JTextField(10);
    private JTextField name = new JTextField(10);
    private JTextField pass = new JTextField(10);
    private JTextField ind = new JTextField(10);
    private JTextField money = new JTextField(10);
    private JTextField state = new JTextField(10);

    private JButton left = new JButton("<");
    private JButton right = new JButton(">");

    private JButton mod = new JButton("确认修改");

    private PersonDao dao = new PersonDao();
    private List<Person> cache = null;
    private int indexOfCache = 0;

    public UpdateFrame(int width, int height) {
        super(width, height);

        search.setBounds(60,0,width-120,40);
        this.add(search,"查找",100);
        search.setFocusable(true);
        search.addKeyListener(this);

        id.setBounds(60,80,width-120,40);
        name.setBounds(60,130,width-120,40);
        pass.setBounds(60,180,width-120,40);
        ind.setBounds(60,230,(width-120)/2-10,40);
        money.setBounds(60+(width-120)/2+10,230,(width-120)/2-10,40);
        state.setBounds(60,280,width-120,40);
        this.add(id,"账号",100);
        this.add(name,"姓名",100);
        this.add(pass,"密码",100);
        this.add(ind,"身份",100);
        this.add(money,"存钱",100);
        this.add(state,"状态",100);


        left.setBounds(0,(height-40)/2,40,40);
        this.add(left,20);
        right.setBounds(width-40,(height-40)/2,40,40);
        this.add(right,20);

        mod.setBounds(60,380,width-120,48);
        this.add(mod,20);

        left.addActionListener(this);
        right.addActionListener(this);
        mod.addActionListener(this);
        search.setText("按照名字搜索，回车确认");
    }

    public void setValue(Person person){
        id.setText(person.getId());
        name.setText(person.getName());
        pass.setText(person.getPassword());
        ind.setText(person.getInd());
        money.setText(person.getMoney()+"");
        state.setText(person.getState());
        id.setEditable(false);
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {

        if (e.getKeyChar() == KeyEvent.VK_ENTER){
            cache = dao.findByName("%"+search.getText()+"%");
            indexOfCache = 0;
            if (cache != null && cache.size() != 0){
                setValue(cache.get(indexOfCache));
                MassageTip.showMassage(null,"找到"+cache.size()+"个");
            }else {
                MassageTip.showMassage(null,"没有找到");
            }
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }

    @Override
    public void actionPerformed(ActionEvent e) {

        if (cache != null){
            if (e.getSource() == left){
                if (indexOfCache > 0){
                    indexOfCache --;
                    setValue(cache.get(indexOfCache));
                }else{
                    MassageTip.showMassage(null,"前面再没有了");
                }
            }else if (e.getSource() == right){
                if (indexOfCache < cache.size()-1){
                    indexOfCache ++;
                    setValue(cache.get(indexOfCache));
                }else {
                    MassageTip.showMassage(null,"后面再没有了");
                }
            }
        }

        if (e.getSource() == mod){

            if (!StringUtils.isEmpty(id.getText()) && id.getText().length() == 9){
                if (!StringUtils.isEmpty(name.getText())){

                    if (!StringUtils.isEmpty(pass.getText())){
                        if (!StringUtils.isEmpty(ind.getText())
                                && (ind.getText().equals("管理员") || ind.getText().equals("学生"))){
                            if (!StringUtils.isEmpty(state.getText())){
                                Person person = new Person();
                                person.setId(id.getText());
                                person.setName(name.getText());
                                person.setPassword(pass.getText());
                                person.setInd(ind.getText());
                                person.setState(state.getText());
                                if (!StringUtils.isEmpty(money.getText())){
                                    person.setMoney(Integer.parseInt(money.getText()));
                                }else {
                                    person.setMoney(0);
                                }
                                int update = dao.update(person);
                                if (update > 0){
                                    MassageTip.showMassage(null,"修改成功");
                                }else if(update == 0){
                                    MassageTip.showMassage(null,"学号不可修改");
                                }else{
                                    MassageTip.showMassage(null,"修改失败");
                                }
                            }else{
                                MassageTip.showMassage(null,"状态不能为空");
                            }
                        }else{
                            MassageTip.showMassage(null,"身份错误");
                        }
                    }else{
                        MassageTip.showMassage(null,"密码不能为空");
                    }

                }else{
                    MassageTip.showMassage(null,"名称不能为空");
                }
            }else{
                MassageTip.showMassage(null,"账号不能为空");
            }
        }
    }
}